package com.woniuxy.driverschool.controller;

import com.woniuxy.driverschool.entity.*;
import com.woniuxy.driverschool.service.StudentService;
import com.woniuxy.driverschool.service.impl.CredentialManagementServiceImpl;
import com.woniuxy.driverschool.service.impl.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 学员
 * @author shkstart
 * @create 2021-10-09 11:10
 */
@RestController
public class StudentController {
    @Autowired
    StudentService studentService;



    /**
     * 分页查询学员注册状态
     * @param student
     * @param pageIndex
     * @param pageSize
     * @param startTime 开始日期
     * @param endTime   结束日期
     * @return
     */
    @GetMapping("/registerStudents")
    public ResponseResult<PageBean<Student>> registeredStudent(Student student,int pageIndex,int pageSize,
                                                               Date startTime,Date endTime){
        System.out.println("查询条件："+student);
        System.out.println("pageSize："+pageSize);
        System.out.println("pageIndex："+pageIndex);
        int CountStudentsByPage = studentService.selectCountStudentsByPage(student, startTime, endTime, null, null);
        List<Student> students = studentService.selectStudentsByPage(student, pageIndex, pageSize, startTime, endTime, null, null);
        PageBean<Student> pageBean = new PageBean<Student>();
        pageBean.setBeanList(students);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(CountStudentsByPage);
        return new ResponseResult<PageBean<Student>>(pageBean,200,"ok");
    }
    /**
     * 分页查询学员结业状态
     * @param student
     * @param pageIndex
     * @param pageSize
     * @param completeStartTime
     * @param completeEndTime
     * @return
     */
    @GetMapping("/completeStudents")
    public ResponseResult<PageBean<Student>> completeStudents(Student student,int pageIndex,int pageSize,Date completeStartTime,Date completeEndTime){
        System.out.println("查询条件："+student);
        System.out.println("pageSize："+pageSize);
        System.out.println("pageIndex："+pageIndex);
        student.setRegister("已注册");
        int CountStudentsByPage = studentService.selectCountStudentsByPage(student, null, null, completeStartTime, completeEndTime);
        List<Student> students = studentService.selectStudentsByPage(student, pageIndex, pageSize, null, null, completeStartTime, completeEndTime);
        PageBean<Student> pageBean = new PageBean<Student>();
        pageBean.setBeanList(students);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(CountStudentsByPage);
        return new ResponseResult<PageBean<Student>>(pageBean,200,"ok");
    }
    /**
     * 添加注册学生
     * @param student
     * @return
     */
    @PostMapping("/addRegisterStudent")
    public ResponseResult addRegisterStudent(@RequestBody Student student){
        TeachProgramme teachProgramme= new TeachProgramme();
        if(student.getCarType().equals("C1")){
            teachProgramme.setTeachProgrammeId(1);
            student.setTeachProgramme(teachProgramme);
        }else if (student.getCarType().equals("C2")){
            teachProgramme.setTeachProgrammeId(2);
            student.setTeachProgramme(teachProgramme);
        }else if (student.getCarType().equals("B1")){
            teachProgramme.setTeachProgrammeId(3);
            student.setTeachProgramme(teachProgramme);
        }else if (student.getCarType().equals("D1")){
            teachProgramme.setTeachProgrammeId(4);
            student.setTeachProgramme(teachProgramme);
        }else if (student.getCarType().equals("B2")){
            teachProgramme.setTeachProgrammeId(5);
            student.setTeachProgramme(teachProgramme);
        }else if (student.getCarType().equals("E")){
            teachProgramme.setTeachProgrammeId(6);
            student.setTeachProgramme(teachProgramme);
        }
        student.setRegister("已注册");
        if(student.getSid()!=null){
            studentService.updateStudentById(student);
        }else {
            student.setRegisDate(new Date());
            student.setCompleteStatus("待结业");
            studentService.insertStudent(student);
        }
        System.out.println("添加成功："+student);
        return new ResponseResult(null,200,"ok");
    }
    /**
     * 添加未注册学生
     * @param student
     * @return
     */
    @PostMapping("/addNoRegisterStudent")
    public ResponseResult addNoRegisterStudent(@RequestBody Student student){
        TeachProgramme teachProgramme= new TeachProgramme();
        System.out.println("aaa"+student.getCarType());
        if(student.getCarType().equals("C1")){
            teachProgramme.setTeachProgrammeId(1);
            student.setTeachProgramme(teachProgramme);
        }else if (student.getCarType().equals("C2")){
            teachProgramme.setTeachProgrammeId(2);
            student.setTeachProgramme(teachProgramme);
        }else if (student.getCarType().equals("B1")){
            teachProgramme.setTeachProgrammeId(3);
            student.setTeachProgramme(teachProgramme);
        }else if (student.getCarType().equals("D1")){
            teachProgramme.setTeachProgrammeId(4);
            student.setTeachProgramme(teachProgramme);
        }else if (student.getCarType().equals("B2")){
            teachProgramme.setTeachProgrammeId(5);
            student.setTeachProgramme(teachProgramme);
        }else if (student.getCarType().equals("E")){
            teachProgramme.setTeachProgrammeId(6);
            student.setTeachProgramme(teachProgramme);
        }
        student.setRegister("未注册");
        if(student.getSid()!=null){
            studentService.updateStudentById(student);
        }else {
            student.setRegisDate(new Date());
            student.setCompleteStatus("待结业");
            studentService.insertStudent(student);
        }
        System.out.println("添加未注册学员："+student);
        return new ResponseResult(null,200,"ok");
    }

    /**
     * 删除学生
     * @param sid
     * @return
     */
    @DeleteMapping("/student/{sid}")
    public ResponseResult deleteStudent(@PathVariable("sid") Integer sid){
        studentService.deleteStudentById(sid);
        System.out.println("删除成功："+sid);
        return new ResponseResult(null,200,"ok");
    }
    /**
     * 根据ID查询学生
     * @param sid
     * @return
     */
    @GetMapping("/student/{sid}")
    public ResponseResult selectStudentBySid(@PathVariable("sid") Integer sid){
        studentService.selectStudentBySid(sid);
        System.out.println("查询成功："+sid);
        return new ResponseResult(null,200,"ok");
    }
    /**
     * 前端日期串转换成日期类
     * @param request
     * @param binder
     */
    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder ) {
        SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
        dateFormat.setLenient( false );
        binder.registerCustomEditor( Date.class, new CustomDateEditor( dateFormat, false ) );
    }
}
